﻿using IBLL;
using IBLL.Dto;
using IDAL;
using Model;

namespace BLL
{
    public class HodeBLL : IHodeBLL
    {
        public HodeBLL(IBase<Hode> db)
        {
            Db = db;
        }

        public IBase<Hode> Db { get; }
        /// <summary>
        /// 添加折扣劵
        /// </summary>
        /// <param name="hode"></param>
        /// <returns>返回受影响行数</returns>
        public Task<int> AddAsync(Hode hode)
        {
           return Db.AddAsync(hode);
        }
        /// <summary>
        /// 根据编号id删除一条折扣劵信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns>返回受影响行数</returns>
        public Task<int> DeleteAsync(int id)
        {
           return Db.DeleteAsync(id);
        }
        /// <summary>
        /// 展示所有折扣劵
        /// </summary>
        /// <returns>返回受影响行数</returns>
        public IQueryable<Hode> GetAll()
        {
            return Db.GetAll();
        }
        /// <summary>
        /// 条件查询、折扣劵名称模糊查询
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="size">页容量</param>
        /// <param name="name">折扣劵名称</param>
        /// <returns>返回受影响行数</returns>
        public GetHodePageDto<Hode> GetHodePage(int index, int size, string? name)
        {
            var query = Db.GetAll();
            if(!string.IsNullOrEmpty(name))
            {
                query = query.Where(x => x.Cname.Contains(name));
            }
            var c = query.Count();
            var d=query.OrderBy(x=>x.Id).Skip((index-1)*size).Take(size).ToList();
            GetHodePageDto<Hode> dto = new GetHodePageDto<Hode>();
            dto.Count = c;
            dto.Data = d;
            return dto;

        }
    }
}
